import {CELL_WIDTH} from '../Model/ConstValue';

import AudioUtils from "../Utils/AudioUtils";
cc.Class({
    extends: cc.Component,

    properties: {
        bombWhite:{
            default: null,
            type: cc.Prefab
        },
        crushEffect:{
            default: null,
            type: cc.Prefab
        },
        audioUtils:{
            type: AudioUtils,
            default: null
        }
    },

    // use this for initialization
    onLoad: function () {
        this.effectPool = new cc.NodePool();
        for(let i = 0;i < 10;i++)
        {
            let effect = cc.instantiate(this.crushEffect);
            this.effectPool.put(effect);
        }
    },
    playEffects: function(effectQueue){
        if(!effectQueue || effectQueue.length <= 0){
            return ;
        }
        //let soundMap = {}; //某一时刻，某一种声音是否播放过的标记，防止重复播放
        effectQueue.forEach(function(cmd){
            let delayTime = cc.delayTime(cmd.playTime);
            let callFunc = cc.callFunc(function(){
                let instantEffect = null;
                let animation = null;
                if(cmd.action == "crush"){
                    
                    if(this.effectPool.size() > 0)
                    {
                        instantEffect = this.effectPool.get();
                    }else{
                        instantEffect = cc.instantiate(this.crushEffect);
                        this.effectPool.put(instantEffect);
                    }
                    
                    animation  = instantEffect.getComponent(cc.Animation);
                    animation.play("effect");
                    // !soundMap["crush" + cmd.playTime] && this.audioUtils.playEliminate(cmd.step);
                    // soundMap["crush" + cmd.playTime] = true;
                }
                else if(cmd.action == "rowBomb"){
                    instantEffect = cc.instantiate(this.bombWhite);
                    animation  = instantEffect.getComponent(cc.Animation);
                    animation.play("effect_line");
                }
                else if(cmd.action == "colBomb"){
                    instantEffect = cc.instantiate(this.bombWhite);
                    animation  = instantEffect.getComponent(cc.Animation);
                    animation.play("effect_col");
                }

                instantEffect.x = CELL_WIDTH * (cmd.pos.x - 0.5);
                instantEffect.y = CELL_WIDTH * (cmd.pos.y - 0.5);
                instantEffect.parent = this.node;
                animation.on("finished",function(){
                    //instantEffect.destroy();
                    this.effectPool.put(instantEffect);
                },this);
               
            },this);
            this.node.runAction(cc.sequence(delayTime, callFunc));
        },this);
    },
});
